<?php

/**
 * @author Martin Pernica
 * @copyright 2008
 */

Text::h1( Lang::getBack('com_poll', 'admin') );

if (empty($_GET['a'])) {

    $query = Registry::get('db')->query("SELECT * FROM poll ORDER BY date DESC");
    echo '<table cellspacing="20" cellpadding="30">';
    echo '<tr><td><form action="' . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'] .
        '&a=new" method="post"><img src="../libs/icons/add.png" /><input type="submit" value="'.Lang::getBack('com_poll', 'new').'" /><input type="text" name="answers" size="30" value="'.Lang::getBack('com_poll', 'answers_count').'" onfocus="this.value = \'\'; this.onfocus = undefined;"/></a></td></tr>';
    echo '<tr><th>'.Lang::getBack('com_poll', 'question').'</th><th>'.Lang::getBack('com_poll', 'answers_count2').'</th><th>'.Lang::getBack('com_poll', 'votes_count').'</th><th>'.Lang::getBack('com_poll', 'date').'</th><th>'.Lang::getBackGlobal('action').'</th></tr>';
    
    while ($data = $query->fetch_array()) {
        $count_answers = count(unserialize($data['answers']));
        $vote = unserialize($data['vote']);
        $lengh = count($vote);
        for ($i = 1; $i <= $lengh; $i++) {
            $count_vote += $vote[$i];
        }
        echo '<tr><td>' . $data['question'] . '</td><td>' . $count_answers . '</td><td>' .
            $count_vote . '</td><td>' . $data['date'] . '</td><td><a href="' . $_SERVER['PHP_SELF'] .
            '?' . $_SERVER['QUERY_STRING'] . '&a=delete&id=' . $data['id'] .
            '"><img src="../libs/icons/delete.png" /></a>&nbsp;&nbsp;<a href="' . $_SERVER['PHP_SELF'] .
            '?' . $_SERVER['QUERY_STRING'] . '&a=edit&id=' . $data['id'] .
            '"><img src="../libs/icons/style_edit.png" /></a></td></tr>';
    }
    echo '</table>';

} else {
    switch ($_GET['a']) {
        case 'new':
            require 'new.php';
            break;

        case 'delete':
            require 'delete.php';
            break;

        case 'edit':
            require 'edit.php';
            break;
    }
}
?>